package com.gzw.dms.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import com.baidu.android.pushservice.PushMessageReceiver;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.bean.Constant;
import com.gzw.dms.bean.PushInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/18.
 */
public class PushMessageActivity extends PushMessageReceiver {
    private Context mContext;
    private String channelId;
    private boolean IsFist=true;
    private final String tag="PushReceiver---->";
    private LoginTask loginTask;

    @Override
    public void onBind(Context context, int errorCode, String appid,
                       String userId, String _channelId, String requestId) {
        String responseString = "onBind errorCode=" + errorCode + " appid="
                + appid + " userId=" + userId + " channelId=" + _channelId
                + " requestId=" + requestId;
        Log.d(TAG, responseString);

        if (errorCode == 0) {
            // 绑定成功
            Log.d(TAG, "绑定成功");

        }

        mContext=context;
        channelId=_channelId;
        SharedPreferences  sharedPreferences=mContext.getSharedPreferences("UserInfo",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("pushuserId", userId);
        edit.putString("channelId", channelId);
        edit.putString("requestId", requestId);
        edit.putString("errorCode",String.valueOf(errorCode));
        edit.putString("appid", appid);
        edit.commit();//
        if(IsFist){
            IsFist=false;
        }

        loginTask = new LoginTask();
        loginTask.execute(channelId);

        // Demo更新界面展示代码，应用请在这里加入自己的处理逻辑
    }

    @Override
    public void onUnbind(Context context, int i, String s) {

    }

    @Override
    public void onSetTags(Context context, int i, List<String> list, List<String> list1, String s) {

    }

    @Override
    public void onDelTags(Context context, int i, List<String> list, List<String> list1, String s) {

    }

    @Override
    public void onListTags(Context context, int i, List<String> list, String s) {

    }

    @Override
    public void onMessage(Context context, String s, String s1) {

    }

    @Override
    public void onNotificationClicked(Context context, String s, String s1, String s2) {

        mContext=context;
        String strs = s;
        String strs1 = s1;
        String strs2 = s2;

        String successMsg = JsonUtil.getMsg(s2, "successMsg");
        PushInfo aaaaaaaa = JsonUtil.getPerson(successMsg, PushInfo.class);
        String type = aaaaaaaa.getType();
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("USERINFO_KEY",
                Context.MODE_PRIVATE);
        UserInfo userInfo = new UserInfo();
        userInfo.setFlag(sharedPreferences.getString("flag", ""));
        userInfo.setPassword(sharedPreferences.getString("passWord", ""));
        userInfo.setUsername(sharedPreferences.getString(Constant.USER_NAME_KEY, ""));
        userInfo.setId(sharedPreferences.getString(Constant.USER_ID_KEY, ""));
        userInfo.setDriver_id(sharedPreferences.getString(Constant.DRIVER_ID_KEY, ""));
        userInfo.setSuppliers_id(sharedPreferences.getString(Constant.SUPPLIERS_ID_KEY, ""));
        userInfo.setFlag(sharedPreferences.getString(Constant.FLAG_KEY, ""));
        userInfo.setRealName(sharedPreferences.getString(Constant.NAME_KEY, ""));
        //本地读取list
        String listStr = sharedPreferences.getString("role", "");
        Gson gson = new Gson();
        List<String> list = gson.fromJson(listStr, new TypeToken<List<String>>()
        {}.getType());
        userInfo.setUserAppRole(list);
        MyApplication.setIsUserLogin(sharedPreferences.getBoolean(Constant.IS_USER_LOGIN_KEY, false));
        MyApplication.setFlag(1);
        MyApplication.setUserInfo(userInfo);
        String flag = userInfo.getFlag().toString().trim();
        if (type.equals("1")) {
            Intent mIntetn = new Intent();
            Bundle bundle = new Bundle();
            mIntetn.setClass(context.getApplicationContext(), UnusualListActivity.class);
            mIntetn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mIntetn.putExtras(bundle);

            context.getApplicationContext().startActivity(mIntetn);

        } else if (type.equals("0")) {
            Intent mIntetn = new Intent();
            Bundle bundle = new Bundle();
            mIntetn.putExtra("fpstste", "2");
            mIntetn.setClass(context.getApplicationContext(), Main2Activity.class);
            mIntetn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            mIntetn.putExtras(bundle);
            context.getApplicationContext().startActivity(mIntetn);
        }else {
            return;
        }
        return;

    }

    @Override
    public void onNotificationArrived(Context context, String s, String s1, String s2) {


    }

    public class LoginTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(String... params) {

            UserInfo userInfo = MyApplication.getUserInfo();
            String iD = userInfo.getId().toString().trim();

            Map<String, String> m = new HashMap<>();
            m.put("id", iD);
            m.put("channelId", channelId);
            m.put("device_type", "3");
            String json = JsonUtil.toJson(m);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appUpdateChannelId"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            loginTask = null;
            if (result == null || result.length() == 0) {
                T.showShort(mContext, "连接服务器异常。");
            }
            if (result.indexOf("successMsg") > 0) {
                String successMsg = JsonUtil.getMsg(result, "successMsg");

            }
        }
    }
//    public String appUpdateChannelId() {
//
//       return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appUpdateChannelId"), "json=" + json);
//    }
}

